package programmercarl.回溯.C2;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution {

    private List<List<Integer>> ans = new ArrayList<>();
    private LinkedList<Integer> curr = new LinkedList<>();

    public List<List<Integer>> combine(int n, int k) {
        backTrack(n, k, 1);
        return ans;
    }

    public void backTrack(int n, int k, int startIndex) {
        if (curr.size() == k) {
            ans.add(new ArrayList<>(curr));
        }
        //从startIndex开始遍历
        for (int i = startIndex; i <= n; i++) {
            curr.add(i);
            backTrack(n, k, i + 1);//若pop 1，那么下一层搜索是从2开始搜，但是i也是2
            curr.removeLast();
        }
    }
}